import { DataTypes, Model } from 'sequelize';
import sequelize from '../db';

class Plan extends Model {
  public id!: string;
  public name!: string;
  public code!: string;
  public description?: string;
  public price!: number;
  public billingCycleCode!: string;
  public priceMonthly!: number;
  public priceYearly!: number;
  public maxUsers?: number;
  public maxStorage?: number;
  public maxProjects?: number;
  public supportLevel!: string;
  public hasApiAccess!: boolean;
  public hasCustomDomain!: boolean;
  public hasWhiteLabel!: boolean;
  public hasSso!: boolean;
  public hasAdvancedAnalytics!: boolean;
  public isActive!: boolean;
  public sortOrder!: number;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

Plan.init(
  {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false,
    },
    code: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    price: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      defaultValue: 0,
    },
    billingCycleCode: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: 'monthly',
      field: 'billing_cycle_code',
    },
    priceMonthly: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      defaultValue: 0,
      field: 'price_monthly',
    },
    priceYearly: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      defaultValue: 0,
      field: 'price_yearly',
    },
    maxUsers: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'max_users',
    },
    maxStorage: {
      type: DataTypes.BIGINT,
      allowNull: true,
      field: 'max_storage',
    },
    maxProjects: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'max_projects',
    },
    supportLevel: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: 'basic',
      field: 'support_level',
    },
    hasApiAccess: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      field: 'has_api_access',
    },
    hasCustomDomain: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      field: 'has_custom_domain',
    },
    hasWhiteLabel: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      field: 'has_white_label',
    },
    hasSso: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      field: 'has_sso',
    },
    hasAdvancedAnalytics: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      field: 'has_advanced_analytics',
    },
    isActive: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      field: 'is_active',
    },
    sortOrder: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      field: 'sort_order',
    },
  },
  {
    sequelize,
    tableName: 'plans',
    underscored: true,
    timestamps: true,
  },
);

export default Plan;

